package com.learn.project2.service.admin.brand;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.learn.project2.bean.MarketBrand;
import com.learn.project2.bean.MarketBrandExample;
import com.learn.project2.mapper.MarketBrandMapper;
import com.learn.project2.model.bo.admin.brand.BrandCreateBO;
import com.learn.project2.model.bo.admin.brand.BrandDeleteBO;
import com.learn.project2.model.bo.admin.brand.BrandListBO;
import com.learn.project2.model.bo.admin.brand.BrandUpdateBO;
import com.learn.project2.model.vo.admin.brand.BrandCreateVO;
import com.learn.project2.model.vo.admin.brand.BrandListVO;
import com.learn.project2.model.vo.admin.brand.BrandUpdateVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author 亓永杰
 * @version 1.0
 * @description: TODO 商场管理-品牌制造商
 * @date 2022-01-08 10:22
 */
@Service
public class BrandServiceImpl implements BrandService {

    @Autowired
    MarketBrandMapper BrandMapper;

    /**
     * @description: 查询品牌商（id精准查询，name模糊查询）
     * @param: BrandListBO
     * @return: BrandListVO
     * @author 亓永杰
     * @date: 2022-01-08 10:24
     */
    @Override
    public BrandListVO listBrands(BrandListBO brandListBO) {
        //获取页码页面大小
        PageHelper.startPage(brandListBO.getPage(), brandListBO.getLimit());

        brandListBO.setOrder("`" + brandListBO.getOrder() + "`");

        MarketBrandExample brandExample = new MarketBrandExample();
        MarketBrandExample.Criteria criteria = brandExample.createCriteria();
        if (brandListBO.getId() != null) criteria.andIdEqualTo(brandListBO.getId());
        if (brandListBO.getName() != null) criteria.andNameLike("%" + brandListBO.getName() + "%");
        //查找未删除的品牌商
        criteria.andDeletedEqualTo(false);
        List<MarketBrand> marketBrands = BrandMapper.selectByExample(brandExample);

        PageInfo<MarketBrand> pageInfo = new PageInfo<>(marketBrands);
        List<MarketBrand> list = pageInfo.getList();
        ArrayList<BrandListVO.ListBean> brandVOS = new ArrayList<>();
        for (MarketBrand marketBrand : list) {
            BrandListVO.ListBean brandVO = new BrandListVO.ListBean();
            brandVO.setId(marketBrand.getId());
            brandVO.setAddTime(marketBrand.getAddTime());
            brandVO.setDeleted(marketBrand.getDeleted());
            brandVO.setDesc(marketBrand.getDesc());
            brandVO.setUpdateTime(marketBrand.getUpdateTime());
            brandVO.setFloorPrice(marketBrand.getFloorPrice());
            brandVO.setName(marketBrand.getName());
            brandVO.setSortOrder(marketBrand.getSortOrder());
            brandVO.setPicUrl(marketBrand.getPicUrl());
            brandVOS.add(brandVO);
        }

        BrandListVO brandListVO = new BrandListVO();
        brandListVO.setLimit(brandListBO.getLimit());
        brandListVO.setPage(brandListBO.getPage());
        brandListVO.setPages((int) Math.ceil((double) pageInfo.getTotal() / brandListBO.getLimit()));
        brandListVO.setTotal((int) pageInfo.getTotal());
        brandListVO.setList(brandVOS);
        return brandListVO;
    }

    /**
     * @description: 添加品牌商
     * @param: BrandCreateBO
     * @return: BrandCreateVO
     * @author 亓永杰
     * @date: 2022-01-08 10:24
     */
    @Override
    public BrandCreateVO insertBrand(BrandCreateBO brandCreateBO) {
        MarketBrand brand = new MarketBrand();
        brand.setDesc(brandCreateBO.getDesc());
        brand.setFloorPrice(brandCreateBO.getFloorPrice());
        brand.setName(brandCreateBO.getName());
        brand.setPicUrl(brandCreateBO.getPicUrl());
        brand.setAddTime(new Date());
        brand.setUpdateTime(new Date());
        brand.setId(null);

        int i = BrandMapper.insertSelective(brand);

        BrandCreateVO brandCreateVO = new BrandCreateVO();
        brandCreateVO.setAddTime(brand.getAddTime());
        brandCreateVO.setUpdateTime(brand.getUpdateTime());
        brandCreateVO.setDesc(brand.getDesc());
        brandCreateVO.setName(brand.getName());
        brandCreateVO.setId(brand.getId());
        brandCreateVO.setFloorPrice(brand.getFloorPrice());
        brandCreateVO.setPicUrl(brand.getPicUrl());

        return brandCreateVO;
    }

    @Override
    public BrandUpdateVO updateBrand(BrandUpdateBO brandUpdateBO) {
        MarketBrand brand = new MarketBrand();
        brand.setId(brandUpdateBO.getId());
        brand.setPicUrl(brandUpdateBO.getPicUrl());
        brand.setAddTime(brandUpdateBO.getAddTime());
        brand.setUpdateTime(new Date(0));
        brand.setName(brandUpdateBO.getName());
        brand.setFloorPrice(brandUpdateBO.getFloorPrice());
        brand.setDesc(brandUpdateBO.getDesc());
        brand.setSortOrder(brandUpdateBO.getSoftOrder());

        BrandMapper.updateByPrimaryKeySelective(brand);

        BrandUpdateVO brandUpdateVO = new BrandUpdateVO();
        brandUpdateVO.setAddTime(brand.getAddTime());
        brandUpdateVO.setUpdateTime(brand.getUpdateTime());
        brandUpdateVO.setDeleted(brand.getDeleted());
        brandUpdateVO.setFloorPrice(brand.getFloorPrice());
        brandUpdateVO.setDesc(brand.getDesc());
        brandUpdateVO.setId(brand.getId());
        brandUpdateVO.setPicUrl(brand.getPicUrl());
        brandUpdateVO.setSortOrder(brand.getSortOrder());
        brandUpdateVO.setName(brand.getName());

        return brandUpdateVO;
    }
/**
 * @description: 逻辑删除
 * @param: BrandDeleteBO
 * @return:  void
 * @author 亓永杰
 * @date: 2022-01-08 14:48
 */
    @Override
    public void deleteBrand(BrandDeleteBO brandDeleteBO) {
        MarketBrand brand = new MarketBrand();
        brand.setId(brandDeleteBO.getId());
        brand.setDeleted(true);
        int i = BrandMapper.updateByPrimaryKeySelective(brand);
    }
}
